package com.freak.neteasecloudmusic.modules.video.fragment;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.app.App;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpFragment;
import com.freak.neteasecloudmusic.modules.video.entity.VideoEntity;
import com.freak.neteasecloudmusic.modules.video.fragment.adapter.VideoAdapter;
import com.freak.neteasecloudmusic.view.recycler.decoration.RecycleViewDivider;
import com.freak.neteasecloudmusic.view.video.MyJzVideoPlay;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * Created by Freak on 2020/3/7.
 */
public class VideoTabFragment extends BaseAbstractMvpFragment<VideoTabPresenter> implements VideoTabContract.View {
    @BindView(R.id.recyclerViewVideo)
    RecyclerView recyclerViewVideo;
    private VideoAdapter mVideoAdapter;
    private Bundle mBundle;
    private int id;
    private List<VideoEntity.DatasBean> mList;

    @Override
    protected VideoTabPresenter createPresenter() {
        return new VideoTabPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video_tab;
    }

    @Override
    protected void initEventAndData() {
        mPresenter.getVideo(String.valueOf(id));
    }

    @Override
    protected void initView(View view) {
        mBundle = getArguments();
        if (mBundle == null) {
            mActivity.finish();
            return;
        }
        id = mBundle.getInt("id");
        mList = new ArrayList<>();
        recyclerViewVideo.setLayoutManager(new LinearLayoutManager(mActivity));
        mVideoAdapter = new VideoAdapter(R.layout.view_item_video, mList, mActivity);
        mVideoAdapter.bindToRecyclerView(recyclerViewVideo);
        mVideoAdapter.setEmptyView(R.layout.view_no_data_common, recyclerViewVideo);
        recyclerViewVideo.setAdapter(mVideoAdapter);
        //添加自定义分割线：可自定义分割线高度和颜色
        recyclerViewVideo.addItemDecoration(new RecycleViewDivider(
                mActivity, LinearLayoutManager.HORIZONTAL, 15, getResources().getColor(R.color.color_e6e6e6)));
        //缺少这里的处理，则滑动视频不会自动暂停
        recyclerViewVideo.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                MyJzVideoPlay jzvd = view.findViewById(R.id.videoTabItemJzVideo);
                if (jzvd != null && Jzvd.CURRENT_JZVD != null &&
                        jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                    if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                        Jzvd.releaseAllVideos();
                    }
                }
            }
        });
    }

    @Override
    protected void initLazyData() {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public void showToast(String toast) {

    }

    @Override
    public void getVideoSuccess(VideoEntity videoEntity) {
        mList.clear();
        if (videoEntity != null) {
            mList.addAll(videoEntity.getDatas());
        }
        mVideoAdapter.notifyDataSetChanged();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        App.resetDensity(mActivity, App.DESIGN_WIDTH);
    }
}
